package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.common;

import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.sap.MnSapIf004V2ServiceImpl;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationadaptersdk.model.dto.CheckPilotByFactoryCodeAndInventoryCodeRequest;
import com.epoch.app.otsapplicationadaptersdk.model.dto.CheckPilotByLogicWarehouseCodeRequest;
import com.epoch.app.otsapplicationadaptersdk.service.OtsApplicationAdapterSdkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author wxy
 * @since 2024/2/5
 */
@Service
public class WarehousePilotService {

    private final Log log = Log.getLogger(WarehousePilotService.class);

    @Resource
    private OtsApplicationAdapterSdkService otsApplicationAdapterSdkService;

    /**
     * 根据工厂编码 + 库存地点 判断仓是否在试点范围
     *
     * @param factoryCode   工厂编码
     * @param warehouseCode 库存地点
     * @return
     */
    public boolean isInPilotScope(String factoryCode, String warehouseCode) {
        CheckPilotByFactoryCodeAndInventoryCodeRequest request = new CheckPilotByFactoryCodeAndInventoryCodeRequest();
        if (StringUtils.isNotBlank(factoryCode)) {
            request.setFactoryCode(factoryCode);
        }
        if (StringUtils.isNotBlank(warehouseCode)) {
            request.setInventoryCode(warehouseCode);
        }
        log.info("checkPilotByFactoryCodeAndInventoryCode request={}", JSON.toJSONString(request));
        Result<Boolean> result = otsApplicationAdapterSdkService.checkPilotByFactoryCodeAndInventoryCode(request);
        log.info("checkPilotByFactoryCodeAndInventoryCode result={}", JSON.toJSONString(result));
        return Optional.ofNullable(result).map(Result::getResult).orElse(Boolean.FALSE);
    }

    /**
     * 根据仓库编码 判断仓库是否在试点范围
     *
     * @param logicWarehouseCode 仓库编码
     * @return
     */
    public boolean isInPilotScope(String logicWarehouseCode) {
        CheckPilotByLogicWarehouseCodeRequest request = new CheckPilotByLogicWarehouseCodeRequest();
        if (StringUtils.isNotBlank(logicWarehouseCode)) {
            request.setLogicWarehouseCode(logicWarehouseCode);
        }
        log.info("checkPilotByLogicWarehouseCode request={}", JSON.toJSONString(request));
        Result<Boolean> result = otsApplicationAdapterSdkService.checkPilotByLogicWarehouseCode(request);
        log.info("checkPilotByLogicWarehouseCode result={}", JSON.toJSONString(result));
        return Optional.ofNullable(result).map(Result::getResult).orElse(Boolean.FALSE);
    }


}
